﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BawangMemo.BackendModels;
using System.IO;

namespace BawangMemo.BawangMemoView.View
{
    /// <summary>
    /// WordView.xaml 的交互逻辑
    /// </summary>
    public partial class WordView : UserControl
    {

        #region Variables

        Dictionary<string, string> _contents = new Dictionary<string, string>();

        #endregion

        #region Properties

        public string Spell
        {
            set
            {
                label1.Content = value;
            }
            get
            {
                return label1.Content.ToString();
            }
        }

        public int Rate
        {
            set
            {
                rateView.Rate = value;
            }
            get
            {
                return rateView.Rate;
            }
        }

        public List<string> Tags
        {
            set
            {
                tagListView.Tags = value;
            }
            get
            {
                return tagListView.Tags;
            }
        }

        public Dictionary<string, string> Contents
        {
            set
            {
                _contents = value;
                UpdateWordContents(_contents);
            }
            get
            {
                return _contents;
            }
        }

        public event RoutedEventHandler TagEventHandler
        {
            add
            {
                tagListView.TagEventHandler += value;
            }
            remove
            {
                tagListView.TagEventHandler -= value;
            }
        }

        #endregion

        #region Methods

        public WordView()
        {
            InitializeComponent();
        }

        public void UpdateWordContents(Dictionary<string, string> contents)
        {
            stackPanel1.Children.Clear();

            bool backgrounded = true;
            foreach (KeyValuePair<string, string> kvp in contents)
            {
                Expander expander = new Expander();
                expander.Header = kvp.Key;
                TextBlock block = new TextBlock();
                block.Text = kvp.Value + "\n";
                block.TextWrapping = TextWrapping.Wrap;
                block.Padding = new Thickness(20, 5, 20, 5);
                expander.Content = block;
                expander.FontSize = 16;
                if (backgrounded)
                    expander.Background = (Brush)FindResource("SkinColor");
                backgrounded = !backgrounded;
                expander.IsExpanded = true;
                stackPanel1.Children.Add(expander);
            }

            string thisword = label1.Content.ToString().ToLower();
            string wordpath = @"AppData\newvoice\" + thisword[0] + @"\" + label1.Content.ToString().ToLower() + ".wav";

            if (File.Exists(wordpath))
            {
                try
                {
                    System.Media.SoundPlayer sndPlayer = new System.Media.SoundPlayer(wordpath);
                    sndPlayer.Play();
                    button1.IsEnabled = true;
                    button1.Visibility = Visibility.Visible;
                }
                catch (Exception)
                {
                    button1.IsEnabled = false;
                    button1.Visibility = Visibility.Hidden;
                }
            }
            else
            {
                button1.IsEnabled = false;
                button1.Visibility = Visibility.Hidden;
            }
        }

        #endregion

        #region Backend Logics

        WordWrapper cachedWord;

        internal void ToggleExplanations(string[] expNames, bool isExpanded)
        {
            foreach (Expander expander in stackPanel1.Children)
            {
                if (expNames.Contains(expander.Header))
                {
                    expander.IsExpanded = isExpanded;
                }
            }
        }

        internal void SetWord(WordWrapper wordWrapper)
        {
            if (wordWrapper == null)
            {
                throw new ApplicationException("wordWrapper is null.");
            }
            cachedWord = wordWrapper;
            WordCard wc = wordWrapper.GetWordCard();
            this.Spell = wc.Spell;
            this.Tags = TagRepository.ListWordTags(wordWrapper);
            this.Rate = (int)(wordWrapper.WordInfo.TotalProficiency * 5);
            this._contents.Clear();
            FillView(wc);
            UpdateWordContents(_contents);
            ToggleExplanations(_contents.Keys.ToArray(), false);
        }

        private void FillView(WordCard wc)
        {
            this._contents.Add("解释", (wc.Meaning));
            if (ViewUtil.IsNotNullOrEmpty(wc.Remember))
            {
                this._contents.Add("助记", (wc.Remember));
            }
            if (ViewUtil.IsNotNullOrEmpty(wc.Synonym))
            {
                this._contents.Add("同义词", (wc.Synonym));
            }
            if (ViewUtil.IsNotNullOrEmpty(wc.Antonym))
            {
                this._contents.Add("反义词", (wc.Antonym));
            }
            if (ViewUtil.IsNotNullOrEmpty(wc.Leifan))
            {
                this._contents.Add("类反", (wc.Leifan));
            }
            if (ViewUtil.IsNotNullOrEmpty(wc.Derivation))
            {
                this._contents.Add("派生词", (wc.Derivation));
            }
            if (ViewUtil.IsNotNullOrEmpty(wc.Place))
            {
                this._contents.Add("出处", (wc.Place));
            }
            if (ViewUtil.IsNotNullOrEmpty(wc.Webster))
            {
                this._contents.Add("Webster", (wc.Webster));
            }
        }

        internal WordWrapper GetWord()
        {
            return cachedWord;
        }

        #endregion

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            string thisword = label1.Content.ToString().ToLower();
            string wordpath = @"AppData\newvoice\" + thisword[0] + @"\" + label1.Content.ToString().ToLower() + ".wav";

            if (File.Exists(wordpath))
            {
                System.Media.SoundPlayer sndPlayer = new System.Media.SoundPlayer(wordpath);
                sndPlayer.Play();
            }
            button1.Focusable = false;
        }

        private void button1_GotFocus(object sender, RoutedEventArgs e)
        {
            //
        }
    }
}
